package com.eorchis.module.statistics.trainingcompletionrate.ui.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.constants.RoleConstants;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.role.domain.Role;
import com.eorchis.module.statistics.trainingcompletionrate.bean.TrainingCompletionRateExport;
import com.eorchis.module.statistics.trainingcompletionrate.bean.TrainingCompletionRateSheet;
import com.eorchis.module.statistics.trainingcompletionrate.service.ITrainingCompletionRateService;
import com.eorchis.module.statistics.trainingcompletionrate.ui.commond.TrainingCompletionRateQueryCommond;
import com.eorchis.module.statistics.trainingcompletionrate.ui.commond.TrainingCompletionRateValidCommond;
import com.eorchis.utils.excelutil.ExcelIO;
import com.eorchis.utils.excelutil.ExcelIOFactory;
import com.eorchis.utils.excelutil.export.bo.Sheet;

/**
 * 
 * Title: TrainingCompletionRateController.java<br>
 * Description: 调训完成率统计<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * 
 * @author SONGHQ
 * @createDate 2016年1月7日
 * @version $Revision$
 */
@RequestMapping(value = TrainingCompletionRateController.MODULE_PATH)
@Controller("trainingCompletionRateController")
public class TrainingCompletionRateController {

	public static final String MODULE_PATH = "/module/statistics";

	@Autowired
	@Qualifier("com.eorchis.module.statistics.trainingcompletionrate.service.impl.TrainingCompletionRateServiceImpl")
	private ITrainingCompletionRateService service;
	/**
	 * @category 查询参学单位调训完成率统计
	 * @param querCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("queryTrainingCompletionRate")
	public String queryTrainingCompletionRate(
			@ModelAttribute("resultList") TrainingCompletionRateQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		List<TrainingCompletionRateValidCommond> queryResultList = queryResultList(queryCommond,request);
		queryCommond.setResultList(queryResultList);
		queryCommond.setCount(queryResultList.size());
		return "";
	}
	
	/**
	 * @category 导出参学单位调训完成率统计
	 * @param queryCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="exportTrainingCompletionRate")
	public String exportTrainingCompletionRate(@ModelAttribute("resultList") TrainingCompletionRateQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception{
		TrainingCompletionRateExport export = new TrainingCompletionRateExport();
		//根据查询条件查询汇总导出结果对象
		export.setResultList(queryResultList(queryCommond,request));
		TrainingCompletionRateSheet sheet = new TrainingCompletionRateSheet();
		sheet.setExportObject(export);
		Sheet[] sheetArray = new Sheet[] { sheet};// 导出Excel的Sheet集合
		ExcelIO excelio = ExcelIOFactory.getExcelIO();// Excel导出工具
		String title=sheet.getSheetName();
		response.setContentType("application/msexcel;charset=ISO8859-1");
		response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(title, "UTF-8") + ".xls");
		OutputStream out = response.getOutputStream();
		excelio.exportExcel(out, "resumeExportStyle", sheetArray, false);
		return null;
	}
	//查询调训完成率统计
	private List<TrainingCompletionRateValidCommond> queryResultList(TrainingCompletionRateQueryCommond queryCommond,HttpServletRequest request) throws Exception{
		Role currentRole = (Role)request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
		Department currentScope = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		if(currentRole==null||currentScope==null){
			return new ArrayList<TrainingCompletionRateValidCommond>();
		}
		String roleCode = currentRole.getCode();
		if(RoleConstants.ROLE_CODE_BW_ADMIN.equals(roleCode)){//部委
			queryCommond.setSearchMinistrieID(currentScope.getDeptID());
				
		}else if(RoleConstants.ROLE_CODE_DW_ADMIN.equals(roleCode)){//单位
			queryCommond.setSearchSignUnitID(currentScope.getDeptID());
		}
		return service.queryTrainingCompletionRateList(queryCommond);
	}
}
